package com.xxl.job.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * index controller
 *
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
public class IndexController {

    @Resource
    private XxlJobService xxlJobService;

    @RequestMapping("/")
    public String index(Model model) {

        Map<String, Object> dashboardMap = xxlJobService.dashboardInfo();
        model.addAllAttributes(dashboardMap);

        return "index";
    }

    @RequestMapping("/chartInfo")
    @ResponseBody
    public ReturnT<Map<String, Object>> chartInfo(Date startDate, Date endDate) {
        ReturnT<Map<String, Object>> chartInfo = xxlJobService.chartInfo(startDate, endDate);
        return chartInfo;
    }

//	@RequestMapping("/toLogin")
//	@PermessionLimit(limit=false)
//	public String toLogin(Model model, HttpServletRequest request) {
//		if (PermissionInterceptor.ifLogin(request)) {
//			return "redirect:/";
//		}
//		return "login";
//	}
//
//	@RequestMapping(value="login", method=RequestMethod.POST)
//	@ResponseBody
//	@PermessionLimit(limit=false)
//	public ReturnT<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember){
//		// valid
//		if (PermissionInterceptor.ifLogin(request)) {
//			return ReturnT.SUCCESS;
//		}
//
//		// param
//		if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)){
//			return new ReturnT<String>(500, I18nUtil.getString("login_param_empty"));
//		}
//		boolean ifRem = (StringUtils.isNotBlank(ifRemember) && "on".equals(ifRemember))?true:false;
//
//		// do login
//		boolean loginRet = PermissionInterceptor.login(response, userName, password, ifRem);
//		if (!loginRet) {
//			return new ReturnT<String>(500, I18nUtil.getString("login_param_unvalid"));
//		}
//		return ReturnT.SUCCESS;
//	}
//
//	@RequestMapping(value="logout", method=RequestMethod.POST)
//	@ResponseBody
//	@PermessionLimit(limit=false)
//	public ReturnT<String> logout(HttpServletRequest request, HttpServletResponse response){
//		if (PermissionInterceptor.ifLogin(request)) {
//			PermissionInterceptor.logout(request, response);
//		}
//		return ReturnT.SUCCESS;
//	}

    @RequestMapping("/help")
    public String help() {

		/*if (!PermissionInterceptor.ifLogin(request)) {
			return "redirect:/toLogin";
		}*/

        return "help";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
